# @Version : 1.0
# @Author  : 刘宇
# @File    : 29_set_detail.py
# @Time    : 2025/10/13 20:54

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}

# 1.len(集合):集合元素个数
print("basket元素的个数是:", len(basket))
# 4

# 2.x in s: 检测x是否为s中的成员
print("apple" in basket)
# True

# 3.add(element):将元素ele添加到集合中
basket.add("grape")
print(basket)
# 填入进去的元素位置是随机的

# 4.remove(element):从集合中移除元素element
basket.remove("apple")
print(basket)

# 5.pop(): 从集合中移除并返回任意一个元素

ele = basket.pop()
print("ele:", ele, "类型是:", type(ele))
# pop() 操作会影响到原集合
print("basket的元素是:", basket)

# 6. union(*others):返回一个新集合
# 全集

books = {'天龙八部', '笑傲江湖'}
books_2 = {'雪山飞狐', '天龙八部', '神雕侠侣'}

books_3 = books.union(books_2)
# books_3=books | books_2
print("books_3:", books_3)  # {'天龙八部', '笑傲江湖', '神雕侠侣', '雪山飞狐'}

# 7. intersection(*others):返回一个新集合
# 交集
books_4 = books.intersection(books_2)
# books_4=books & books_2
print("books_4:", books_4)  # {'天龙八部'}

# 8.difference(*other): 返回一个新集合
# 其中包含原集合中在 others 指定的其他集合中不存在的元素

books = {'天龙八部', '笑傲江湖'}
books_2 = {'雪山飞狐', '天龙八部', '神雕侠侣'}

# 需求:求出只存在books集合的元素
# books_5=books-books_2
books_5=books.difference(books_2)
print("books_5:", books_5)   # {'笑傲江湖'}

